package com.cn.test.util;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Created by Administrator on 2017/10/26.
 */

public class UiUtils {
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private static long lastClickTime;
    private static int clickedView;

    /**
     * @deprecated {@link UiUtils#isNormalClick(View)}
     */
    @Deprecated
    public static boolean isNormalClick() {
        boolean isNormal = false;
        long currentTime = System.currentTimeMillis();
        if ((currentTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            isNormal = true;
        }
        lastClickTime = currentTime;
        return isNormal;
    }

    public static boolean isNormalClick(View view) {
        // hashCode确保同一个view计算出来是一样的，不同view也几乎不会遇到相同hashCode的情况，
        long currentTime = System.currentTimeMillis();
        if (clickedView != view.hashCode()) {
            // 点击不同的view，不限制时间间隔，
            clickedView = view.hashCode();
            lastClickTime = currentTime;
            return true;
        }
        // 同一个view多次点击，限制连续点击时间，
        clickedView = view.hashCode();
        boolean isNormal = false;
        if ((currentTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            isNormal = true;
        }
        lastClickTime = currentTime;
        return isNormal;
    }

    /**
     * point保留几位就是几    1wei 1 二位 2
     *
     * @return
     */
    public static Double KeepPoint1(Double number, int point) {
        BigDecimal bg = new BigDecimal(number);
        double f1 = bg.setScale(point, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    /**
     * 传值   整除倍数  返回有两位小数点的数据
     *
     * @param obj
     * @param number
     * @return
     */
    public static String aliquotNumber(Object obj, int number, int KeepNumber) {
        if (obj == null) {
            return "0";
        }
        String str = "";
        String temp = "";
        int dividend = number;
        int divisor = 0;
        DecimalFormat df;
        if (KeepNumber == 2) {
            df = new DecimalFormat("0.00");
        } else if (KeepNumber == 1) {
            df = new DecimalFormat("0.0");
        } else {
            df = new DecimalFormat("0");
        }
        temp = obj.toString();
        long ldiv = Long.parseLong(temp);
        str = df.format((float) ldiv / dividend);
        return str;
    }

    /**
     * 小于1w 显示原值  大于一万小以一亿 换成万  大于一亿 转换成亿
     *
     * @param number
     * @return
     */
    public static String ShowNumber(String number, int KeepNumber) {
        try {
            if (TextUtils.isEmpty(number)) {
                return "0";
            }
            double doubleNumber = Double.parseDouble(number);
            if (doubleNumber < 10000) {
                return aliquotNumber(Long.parseLong(number), 1, 0);
            } else if (doubleNumber < 100000000) {
                return aliquotNumber(Long.parseLong(number), 10000, KeepNumber) + "万";
            } else {
                return aliquotNumber(Long.parseLong(number), 100000000, KeepNumber) + "亿";
            }
        } catch (Exception E) {
            return "0";
        }
    }


}
